﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class ITXSETUP 
    { 
 
        /*
        #region=====vwMENU=======
        CollectionViewSource vwMENU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMENU");
            }
        }
        DataRow dwMENU
        {
            get
            {
                if (vwMENU.View == null)
                    return null;
                if (SetMaster.ICC_MENUDEF.Rows.Count == 0)
                    return null;
                if ((DataRowView)vwMENU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMENU.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPMENU=======
        CollectionViewSource vwTMPMENU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPMENU");
            }
        }
        DataRow dwTMPMENU
        {
            get
            {
                if (vwTMPMENU.View == null)
                    return null;
                if ((DataRowView)vwTMPMENU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPMENU.View.CurrentItem).Row;
            }
        }
        #endregion*/
        BindingListCollectionView cvDlgMenu
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMenu.ItemsSource);
            }
        }   
        void vwMENU_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMENU == null)
                return;
            txtCurrentUser.Text = dwMENU["USERID"].ToString();
        } 
        
        private void butMenuITXLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadITXMenu(Mandant, company);
                SetMaster.Clear();
                SetMaster.Merge(setResult);
                if (vwMENU.View != null)
                {
                    vwMENU.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }
        private void butMenuLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadMenu();
                SetMaster.Clear();
                SetMaster.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetMaster.Clear();
                SetMaster.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetMaster.AcceptChanges();
            }
        }
        private void butMenuSave_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                m_Data.SaveMenuSetup(SetDetail, "00");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butNewMenu_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwMENU == null)
                    return;
                DataRow dwNewMenu = SetDetail.ICC_MENUDEF.NewICC_MENUDEFRow();
                dwNewMenu.ItemArray =dwMENU.ItemArray;
                dwNewMenu["ROWNID"] = "0";
                SetDetail.ICC_MENUDEF.Rows.Add(dwNewMenu);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void butMenuCopy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                string filter = "USERID='" + txtCurrentUser.Text.ToString() + "'";
                DataRow[] rows = SetMaster.ICC_MENUDEF.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_MENUDEF"; ;
                foreach (DataRow RX in tb.Rows)
                {
                    RX["USERID"] = txtNewUser.Text.ToString();
                    RX["ROWNID"] = "0";
                }
                SetDetail.Clear();
                SetDetail.Merge(tb);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butMenuEdit_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                string filter = "USERID='" + txtCurrentUser.Text.ToString() + "'";
                DataRow[] rows = SetMaster.ICC_MENUDEF.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_MENUDEF"; ;
                SetDetail.Clear();
                SetDetail.Merge(tb);


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                DataRow[] rows;
                string USERID = txtCurrentUser.Text;
                rows = SetMaster.ICC_MENUDEF.Select("USERID = '" + USERID + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
                SetMaster.AcceptChanges();
            }
        }

        
        private void butMenuCopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_MENUDEF);
                foreach (DataRow RX in SetDetail.ICC_MENUDEF.Rows)
                {
                    RX["ROWNID"] = "0";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_MENUDEF);
            }
        }

        private void btnFilterMENU_Click(object sender, RoutedEventArgs e)
        {
            string filter = "USERID ='" + txtCurrentUser.Text + "'";
            cvDlgMenu.CustomFilter = filter;
        }
        private void btnFilterMenuClear_Click(object sender, RoutedEventArgs e)
        {
            cvDlgMenu.CustomFilter = null;
        }
        private void btnFilterMenuHidden_Click(object sender, RoutedEventArgs e)
        {
            m_Data.DeleteHiddenMenu();
        }
        private void cmbFunction_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (dwEFUNCTION == null)
                return;
            if (dwTMPMENU == null)
                return;
            dwTMPMENU["HEADER"] = dwEFUNCTION["DESCRIPTION"].ToString().Trim();
            dwTMPMENU["MENUITEM"] = "cmd" + dwTMPMENU["TAG"].ToString().Trim();
        } 
    }
}
